<?php
include("model/receipt_imports.php");
class imports{
	
	function getImportDetailById($im_id){
		$importDetail = array();
		qdbconn();
		
		$query = "SELECT im_id,emp_name,im_date,sup_name,pay_method,im_status
				  FROM imports
				  	JOIN employees on im_emp_id = emp_id
				  	JOIN suppliers on im_sup_id = sup_id
				  	JOIN payments on im_pay_id = pay_id
				  WHERE im_id = $im_id";
		$result = mysql_query($query);
		
		while ($row = mysql_fetch_row($result)) {
			$importDetail['im_id']		= $row[0];
			$importDetail['emp_name']	= $row[1];
			$importDetail['im_date']	= $row[2];
			$importDetail['sup_name']	= $row[3];
			$importDetail['pay_method']	= $row[4];
			$importDetail['im_status']	= $row[5];
			$receipt_import = new receipt_imports();
			$importDetail['transactions']	= $receipt_import->getAllTransactionsById($im_id);
		}
		return $importDetail;	
	}
	
	function getImportsList(){
		$importsList = array();
		qdbconn();
		
		$query = "Select im_id, im_date, emp_name, sup_name , pay_method,im_status
							from imports i
								LEFT JOIN suppliers s ON s.sup_id=i.im_sup_id
								LEFT JOIN employees e ON i.im_emp_id = e.emp_id 
								LEFT JOIN payments p ON i.im_pay_id = p.pay_id 
							";
		
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$importsList[$i]['im_id']	= $row[0];
			$importsList[$i]['im_date']= $row[1];
			$importsList[$i]['emp_name']	= $row[2];
			$importsList[$i]['sup_name']= $row[3];
			$importsList[$i]['pay_method']	= $row[4];
			$importsList[$i]['im_status']	= $row[5];
			
			$i++;
		}
		return $importsList;
	}
	
	function insertAImport($data,$transaction){
		qdbconn();
		$im_date	 = $data['im_date'];
		$im_emp_id	 = $data['im_emp_id'];
		$im_sup_id	 = $data['im_sup_id'];
		$im_pay_id	 = $data['im_pay_id'];

		$query = "INSERT INTO imports (im_date,im_emp_id,im_sup_id,im_pay_id) 
				values ('$im_date',$im_emp_id,$im_sup_id,$im_pay_id)";
		$result = mysql_query($query);
		
		$query = "SELECT im_id FROM imports order by im_id DESC limit 1";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			$reci_im_id = $row[0];
		}
		
		$query 		= "INSERT INTO receipt_imports VALUES";
		for ($i=1;$i<=3;$i++)
		{
			if ($transaction[$i]['reci_pro_id']!=-1 && $transaction[$i]['reci_propla_id']!=-1)
			{
				$reci_pro_id 	= $transaction[$i]['reci_pro_id'];
				$reci_propla_id = $transaction[$i]['reci_propla_id'];
				$reci_quantity 	= $transaction[$i]['reci_quantity'];
				$reci_price 	= $transaction[$i]['reci_price'];				
				if ($i!=1){
					$query .= ",";
				}
				$query .= " ('',$reci_quantity,$reci_price,$reci_pro_id,$reci_propla_id,$reci_im_id)";
				
				$query_different = "UPDATE product_place 
							SET propla_quantity = propla_quantity+$reci_quantity 
							WHERE propla_pro_id = $reci_pro_id
								AND propla_pla_id = $reci_propla_id";
				mysql_query($query_different);				
			}
		}	
		
		$result = mysql_query($query);	
		return $result;
	}
	
	function inactivateAImport($im_id){
		qdbconn();		
		$query = "UPDATE imports SET im_status = 0 where im_id = $im_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateAImport($im_id){
		qdbconn();		
		$query = "UPDATE imports SET im_status = 1 where im_id = $im_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editAImport($data){
		qdbconn();
		$im_id 		 = $data['im_id'];
		$im_date	 = $data['im_date'];
		$im_emp_id	 = $data['im_emp_id'];
		$im_sup_id	 = $data['im_sup_id'];
		$im_pay_id	 = $data['im_pay_id'];		
		
		$query 		 = "UPDATE imports SET im_date='$im_date', 
						im_emp_id=$im_emp_id,im_sup_id=$im_sup_id,im_pay_id=$im_pay_id
						WHERE im_id = $im_id";

		$result = mysql_query($query);
		return $result;
	}
	
	function getImportsInfo($im_id){
		qdbconn();		
		$query = "SELECT * FROM imports
					WHERE im_id = $im_id";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['im_id'] 			= $row[0];
			$current_data['im_date'] 		= $row[1];
			$current_data['im_emp_id'] 		= $row[2];
			$current_data['im_sup_id'] 		= $row[3];
			$current_data['im_pay_id'] 		= $row[4];
			$current_data['im_status'] 		= $row[5];
		}
		return $current_data;
	
	}
}

?>